import { ref } from 'vue';
import { defineStore } from 'pinia';
import * as Router from '@/router/index'

const useAppStore = defineStore('app', () => {
  // 侧边导航栏的选中展开状态
  const asiderChecked = ref(false);
  // 是否加入角色、权限、登录验证
  const hasPromission = ref(false);
  // 是否已进行了路由添加(用于动态添加路由后放行)
  const isAddRoutes = ref(false);
  // 侧边栏筛选过的动态路由
  const asideRouter = ref([]);
  // 全局加载动画开关
  const loadingFlag = ref(false);
  const loadingContent = ref('Loading...');
  const changeAsiderChecked = flag => {
    asiderChecked.value = flag;
  };
  const changeIsAddRoutes = flag => {
    isAddRoutes.value = flag;
  };
  // 过滤异步路由路由
  const filterRouter = () => {
    // 加载全部的异步路由 / 或者在此处按条件筛选过滤路由
    let { dynamicRoutes } = Router;
    return dynamicRoutes;
  };
  // 修改路由配置
  const changeRouter = Router => {
    let routers = filterRouter();
    asideRouter.value = routers;
    routers.forEach(item => {
      Router.default.addRoute(item);
    });
    changeIsAddRoutes(true);
  }
  // 打开加载动画
  const openLoading = content => {
    loadingContent.value = content;
    loadingFlag.value = true;
  };
  const closeLoading = () => {
    loadingContent.value = 'Loading...';
    loadingFlag.value = false;
  }
  return {
    asideRouter,
    asiderChecked,
    hasPromission,
    isAddRoutes,
    loadingFlag,
    loadingContent,
    filterRouter,
    changeRouter,
    changeIsAddRoutes,
    changeAsiderChecked,
    openLoading,
    closeLoading
  };
});
export default useAppStore;